<?php
defined('BASEPATH') OR exit('No direct script access allowed');

define("TOKEN", "51haopiao"); //TOKEN值
define("APPID", "wx5672506691fea394"); //appid值
define("APPSECRET", "a10b06bf4f7519152bad03311f212e65"); //密钥
class Calculate extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Access_Token_Model');
		$this->load->library('HttpPost');
		$this->load->helper('url_helper');
	}
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 *        http://example.com/index.php/welcome
	 *    - or -
	 *        http://example.com/index.php/welcome/index
	 *    - or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index(){
		$access_token=$this->get_access_token();
		$data=array();
		$data['begin_date'] = '2016-05-23';
		$data['end_date'] = '2016-05-27';
		$url='https://api.weixin.qq.com/datacube/getusercumulate?access_token='.$access_token;
		print_r($url);
		$timeout = 40;
		$dt=json_encode($data);
		$response=$this->httppost->post($url,$dt,$timeout);
		print_r($response);
		$abk['title']='统计分析';
		$abk['content']='统计分析';
		$this->load->view('head_sec',$abk);
		$this->load->view('article_detail',$abk);
		$this->load->view('foot_sec',$abk);
	}

	public function get_access_token(){
		$access_token_row=$this->Access_Token_Model->get_access_token();
		$expire_time=$access_token_row['expire_time'];
		echo  'expire_time is:'.$expire_time;
		if(strtotime($expire_time)<time()){
			$accessToken=$this->get_new_access_token();
			$data=array(
				'id'=>$access_token_row['id'],
				'gmt_modify'=>date("Y-m-d H:i:s" ,time()),
				'expire_time'=>date("Y-m-d H:i:s" ,time()+7200),
				'access_tokens'=>$accessToken
			);
			$result=$this->Access_Token_Model->update_access_token($data);
			if($result){
				return $accessToken;
			}else{
				return "";
			}
		}else{
			return $access_token_row['access_tokens'];
		}

	}
	public function get_new_access_token(){
		$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . APPID . "&secret=" . APPSECRET;
		$res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
//echo $res;
		$result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
		$access_token = $result['access_token'];
		return $access_token;
	}
	public function  expire_access_token(){
		$access_token_row=$this->Access_Token_Model->get_access_token();
		if(!isset($access_token_row)){
			return false;
		}
		$data=array(
			'id'=>$access_token_row['id'],
			'gmt_modify'=>date("Y-m-d H:i:s" ,time()),
			'expire_time'=>date("Y-m-d H:i:s" ,time())
		);
		$result=$this->Access_Token_Model->update_access_token($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
}


